package com.example.mobile2021606144ywr0408;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private CheckBox chkboxMainYingqing;
    private CheckBox chkboxMainBoli;
    private CheckBox chkboxMainFxp;
    private Button btnMainBuy;
    private TextView tvMainGouwuche;
    private TextView tvMainresult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        chkboxMainYingqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainresult.setText("选中" + buttonView.getText().toString());
                }else{
                    tvMainresult.setText("取消选中"+buttonView.getText().toString());
                }
            }
        });
        chkboxMainBoli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainresult.setText("选中" + buttonView.getText().toString());
                }else{
                    tvMainresult.setText("取消选中"+buttonView.getText().toString());
                }
            }
        });
        chkboxMainFxp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainresult.setText("选中" + buttonView.getText().toString());
                }else{
                    tvMainresult.setText("取消选中"+buttonView.getText().toString());
                }
            }
        });
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg="选中";
                if (chkboxMainYingqing.isChecked()){
                    msg+="\n"+chkboxMainYingqing.getText().toString();
                }
                if (chkboxMainBoli.isChecked()){
                    msg+="\n"+chkboxMainBoli.getText().toString();
                }
                if (chkboxMainFxp.isChecked()){
                    msg+="\n"+chkboxMainFxp.getText().toString();
                }
                tvMainresult.setText(msg);
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        chkboxMainYingqing = (CheckBox) findViewById(R.id.chkbox_main_yingqing);
        chkboxMainBoli = (CheckBox) findViewById(R.id.chkbox_main_boli);
        chkboxMainFxp = (CheckBox) findViewById(R.id.chkbox_main_fxp);
        btnMainBuy = (Button) findViewById(R.id.btn_main_buy);
        tvMainGouwuche = (TextView) findViewById(R.id.tv_main_gouwuche);
        tvMainresult = (TextView) findViewById(R.id.tv_mainresult);
    }
}